package com.lsh.action.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lsh.action.AbstractManagerAction;
import com.lsh.model.TsysMenu;
import com.lsh.service.TsysMenuService;

@ParentPackage("test-default")
@Namespace(value = "/system")
@Results( { @Result(name = "success", location = "/system/sysMenuList.jsp"),
		@Result(name = "sysMenuAdd", location = "/system/sysMenuAdd.jsp"),
		@Result(name = "sysMenuEdit", location = "/system/sysMenuEdit.jsp"),
			@Result(name = "sysMenuView", location = "/system/sysMenuView.jsp") })
public class TsysMenuAction extends AbstractManagerAction {
	
	private static final long serialVersionUID = 1L;
	private final static Log LOG = LogFactory.getLog(TsysMenuAction.class);

	@Autowired
	private TsysMenuService tsysMenuService;
	private TsysMenu tsysMenu;
	private String id;
	private String ids;//批量操作的ID
	private String flag;//edit,save
	
	public TsysMenuAction() {
		backUrlHead="/system/sysMenuList.do";
	}
	
	@Action("sysMenuList")
	public String list() {
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			page = tsysMenuService.getPage(map, page);
			LOG.info((page.getPageNo()));
			page.setItems((List<TsysMenu>)page.getItems());
			
			tsysMenu = tsysMenuService.getTree();
			return SUCCESS;  
		} catch (Exception e) {
			e.printStackTrace();
			message="显示列表出错了";
			LOG.error("列表出错了");
			return ERROR;
		}
	}

	@Action("sysMenuAdd")
	public String add() {
		try {
			if(flag==null || "".equals(flag)){
				return "sysMenuAdd";
			}
			tsysMenuService.insert(tsysMenu);
			message= "添加信息成功";
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message="添加信息出错了";
			LOG.error("添加出错了");
			return ERROR;
		}
	}

	@Action("sysMenuDelete")
	public String delete() {
		try {
			tsysMenu = tsysMenuService.getById(String.valueOf(id));
			if(tsysMenu!=null){
				tsysMenuService.delete(tsysMenu);
				message= "删除成功";
			}else{
				message="没有该记录";
			}
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message="删除信息出错了";
			LOG.error("删除出错了");
			return ERROR;
		}
	}
	
	@Action("sysMenuDeleteAll")
	public String deleteAll() {
		try {
			message= "批量删除成功";
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message="批量删除出错了";
			LOG.error("批量删除出错了");
			return ERROR;
		}
	}
	
	@Action("sysMenuEdit")
	public String edit() {
		try {
			if(flag==null || "".equals(flag)){
				tsysMenu=tsysMenuService.getById(String.valueOf(id));
				return "sysMenuEdit";
			}
			tsysMenuService.update(tsysMenu);
			message= "修改信息成功";  
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message= "修改信息出错"; 
			LOG.error("更新出错了"); 
			return ERROR;
		}
	}

	@Action("sysMenuView")
	public String view() {
		try {
			if(id!=null && !id.equals("")){
				tsysMenu=tsysMenuService.getById(String.valueOf(id));
				return "sysMenuView";
			}
			return "sysMenuView";
		} catch (Exception e) {
			e.printStackTrace();
			message= "查看信息出错"; 
			LOG.error("查看出错了"); 
			return ERROR;
		}
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public TsysMenu getTsysMenu() {
		return tsysMenu;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public void setTsysMenu(TsysMenu TsysMenu) {
		this.tsysMenu = TsysMenu;
	}
}
